//给定 n 个非负整数表示每个宽度为 1 的柱子的高度图，计算按此排列的柱子，下雨之后能接多少雨水。 
//
// 
//
// 示例 1： 
//
// 
//
// 
//输入：height = [0,1,0,2,1,0,1,3,2,1,2,1]
//输出：6
//解释：上面是由数组 [0,1,0,2,1,0,1,3,2,1,2,1] 表示的高度图，在这种情况下，可以接 6 个单位的雨水（蓝色部分表示雨水）。 
// 
//
// 示例 2： 
//
// 
//输入：height = [4,2,0,3,2,5]
//输出：9
// 
//
// 
//
// 提示： 
//
// 
// n == height.length 
// 1 <= n <= 2 * 10⁴ 
// 0 <= height[i] <= 10⁵ 
// 
// Related Topics 栈 数组 双指针 动态规划 单调栈 👍 2837 👎 0

package leetcode.editor.cn;

import java.util.Stack;

public class _42_TrappingRainWater {
    public static void main(String[] args) {
        int[] height = new int[]{0,1,0,2,1,0,1,3,2,1,2,1};
        Solution solution = new _42_TrappingRainWater().new Solution();
        System.out.println(solution.trap(height));
    }

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public int trap(int[] height) {
            int left = 0, right = height.length - 1;
            int leftMax = 0, rightMax = 0;
            int res = 0;
            while (left < right) {
                if (height[left] < height[right]) {
                    if (height[left] < leftMax) {
                        res += leftMax - height[left++];
                    } else {
                        leftMax = height[left++];
                    }
                } else {
                    if (height[right] < rightMax) {
                        res += rightMax - height[right--];
                    } else {
                        rightMax = height[right--];
                    }
                }
            }
            return res;
        }
        /**
         * 单调栈
         * @param height
         * @return
         */
        public int trap_stack(int[] height) {
            int res = 0;
            Stack<Integer> st = new Stack<Integer>();
            for (int i = 0; i < height.length; i++) {
                while (!st.empty() && height[i] > height[st.peek()]) {
                    int index = st.pop();
                    if (st.empty()) break;
                    int len = i - st.peek() - 1;
                    int hi = Math.min(height[st.peek()], height[i]) - height[index];
                    res += len * hi;
                }
                st.push(i);
            }
            return res;
        }

        /**
         * 动态规划
         *
         * @param height
         * @return
         */
        public int trap_dp(int[] height) {
            int res = 0;
            int[] leftMax = new int[height.length];
            int tmpMax = 0;
            for (int i = 0; i < height.length; i++) {
                if (height[i] > tmpMax) {
                    tmpMax = height[i];
                }
                leftMax[i] = tmpMax;
            }
            int[] rightMax = new int[height.length];
            tmpMax = 0;
            for (int i = height.length - 1; i >= 0; i--) {
                if (height[i] > tmpMax) {
                    tmpMax = height[i];
                }
                rightMax[i] = tmpMax;
            }
            for (int i = 0; i < height.length; i++) {
                int tmpMin = Math.min(leftMax[i], rightMax[i]);
                res += tmpMin - height[i];
            }
            return res;
        }

        /**
         * 暴力解法
         *
         * @param height
         * @return
         */
        public int trap_baoli(int[] height) {
            int res = 0;
            for (int i = 0; i < height.length; i++) {
                int leftMax = height[i];
                int rightMax = height[i];
                for (int j = 0; j < i; j++) {
                    leftMax = height[j] > leftMax ? height[j] : leftMax;
                }
                for (int j = height.length - 1; j > i; j--) {
                    rightMax = height[j] > rightMax ? height[j] : rightMax;
                }
                res += Math.min(leftMax, rightMax) - height[i];
            }
            return res;
        }

        /**
         * 有错
         *
         * @param height
         * @return
         */
        public int trap_err(int[] height) {
            int res = 0;
            for (int i = 1; i < height.length - 1; i++) {
                int left = i;
                int right = i;
                while (left > 0) {
                    if (height[left - 1] > height[left]) {
                        left--;
                    } else {
                        break;
                    }
                }
                while (right < height.length - 1) {
                    if (height[right + 1] > height[right]) {
                        right++;
                    } else {
                        break;
                    }
                }
                if (left == i || right == i) {
                    continue;
                } else {
                    int max = Math.min(height[left], height[right]);
                    for (int j = left + 1; j < right; j++) {
                        res += (max - height[j]);
                    }
                }
            }
            return res;
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}